package com.bit.forum.controller;

import com.bit.forum.common.AppResult;
import com.bit.forum.common.ResultCode;
import com.bit.forum.config.AppConfig;
import com.bit.forum.model.User;
import com.bit.forum.service.IUserService;
import com.bit.forum.utils.MD5Util;
import com.bit.forum.utils.StringUtil;
import com.bit.forum.utils.UUIDUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 用户注册
 * @Author suhzou
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private IUserService userService;

    @PostMapping("/register")
    public AppResult register(@RequestParam("username") @NonNull String username,
                              @RequestParam("nickname") @NonNull String nickname,
                              @RequestParam("password") @NonNull String password,
                              @RequestParam("passwordRepeat") @NonNull String passwordRepeat)
    {
        //非空校验
/*        if (StringUtil.isEmpty(username)||StringUtil.isEmpty(nickname)||
                StringUtil.isEmpty(password)||StringUtil.isEmpty(passwordRepeat)) {
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            return AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }*/
        if(!password.equals(passwordRepeat)) {
            log.info(ResultCode.FAILED_TWO_PWD_NOT_SAME.toString());
            return AppResult.failed(ResultCode.FAILED_TWO_PWD_NOT_SAME);
        }
        User user = new User();
        user.setUsername(username);
        user.setNickname(nickname);

        String salt= UUIDUtil.UUID_32();
        user.setSalt(salt);
        String encryptPassword= MD5Util.md5Salt(password,salt);
        user.setPassword(encryptPassword);
        userService.createNormalUser(user);
        return AppResult.success();
    }

    @PostMapping("/login")
    public AppResult<User> login(HttpServletRequest request,
                                 @RequestParam("username") @NonNull String username,
                                 @RequestParam("password") @NonNull String password){
        User user = userService.login(username, password);
        HttpSession session= request.getSession(true);
        session.setAttribute(AppConfig.USER_SESSION, user);
        return AppResult.success(user);
    }

    @GetMapping("/info")
    public AppResult<User>  getUserInfo(HttpServletRequest request,
                                       @RequestParam(value = "id",required = false) Long id){
        User user=null;
        if(id == null) {
            //如果id为空 从session中获取当前登录用户信息
            HttpSession session = request.getSession(false);
            user = (User) session.getAttribute(AppConfig.USER_SESSION);
        }else{
            user=userService.selectByPrimaryKey(id);
        }
        if(user==null) {
            return AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        return AppResult.success(user);
    }

    @GetMapping("/logout")
    public AppResult<User> logout(HttpServletRequest request){
        //获取session对象
        HttpSession session = request.getSession(false);
        //判断session是否有效
        if(session!=null){
            log.info("退出成功");
            //表示用户在登录状态 直接销毁session
            session.invalidate();
        }
        return AppResult.success("退出成功");
    }
}
